<?php

namespace App\Admin\Controllers;

use App\Models\Student;
use App\Models\Crousesheet;
use App\Models\Attendance_record;
use App\Models\Attendance_report;
use App\Models\Notice;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;

class Attendance_recordController  extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content->header(trans('admin.attendance_record'))
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    public function edit($id,Content $content)
    {
        return $content
            ->header(trans('admin.attendance_record'))
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.attendance_record'))
            ->description(trans('admin.create'))
            ->body($this->form());
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Attendance_record());
        $notice_id = request('notice_id');
        $grid->model()->where('notice_id',$notice_id);
        $grid->id('序号')->sortable();
        $grid->student_id(trans('admin.en_name'))->display(function($student_id){
            return Student::find($this->student_id)->en_name;
        });
        //$grid->attendance_status(trans('admin.attendance_status'))->editable('select', [1 => '正常', 2 => '请假', 3 => '迟到', 4 => '缺勤']);
        $grid->attendance_status(trans('admin.attendance_status'))->display(function($attendance_status){
            if ($this->attendance_status == 1) {
                return "正常";
            }elseif ($this->attendance_status == 2) {
                return "请假";
            }elseif ($this->attendance_status == 3) {
                return "迟到";
            }elseif ($this->attendance_status == 4) {
                return "缺勤";
            }
            elseif ($this->attendance_status == 5) {
                return "家长代课";
            }
            else{
                return 'N/A';
            }
        })->sortable();
        
        $grid->attendance_desc(trans('admin.attendance_desc'));
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            //$actions->disableEdit();
            $actions->disableView();
        });
        $grid->disableCreateButton();
        //$grid->disableActions();
        //禁用导出按钮
        //$grid->disableExport();
        $filename="考勤记录";
        $grid->exporter(new ExcelExpoter($grid,$filename));
        
        $grid->tools(function ($tools) { 
            //关闭批量删除 
            $tools->batch(function ($batch) { 
                $batch->disableDelete(); 
            });
        });
        //$grid->disableFilter();//禁用查询
        return $grid;
    }

    protected function form()
    { 
        $form = new Form(new Attendance_record());
        $form->display('id', 'ID');
        $form->radio('attendance_status')->options([
            1 => '正常',
            2 => '请假',
            3 => '迟到',
            4 => '缺勤',
            5 => '家长代课',
        ]);
        //$form->select('attendance_status', trans('admin.attendance_status'))->options([1=>'正常',2=>'请假',3=>'迟到',4=>'缺勤'])->rules('required')->addElementLabelClass('must-input');
        $form->textarea('attendance_desc',trans('admin.attendance_desc'));
        
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        // $form->saved(function (Form $form) {
        //     $attendance_report_id = $form->model()->attendance_report_id;
        //     // 跳转页面
        //     return redirect('/admin/attendance_report/'.$attendance_report_id);
        //     });
        return $form;
    }
}
